//
//  CircleManagementViewController.m
//  安好
//
//  Created by rlp on 14-11-27.
//  Copyright (c) 2014年 lianchuangbrother. All rights reserved.
//

#import "CircleManagementViewController.h"

@interface CircleManagementViewController ()

@end

@implementation CircleManagementViewController

-(void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [[AFHTTPRequestOperationManager manager].operationQueue cancelAllOperations];
    [self hiddenActionView];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    if (!self.circleArray) {
        self.circleArray = [[NSMutableArray alloc]initWithCapacity:10];
    }
    if (!self.selectDict) {
        self.selectDict = [[NSMutableDictionary alloc]initWithCapacity:10];
        
    }
    if (!self.attentionDict) {
        self.attentionDict = [[NSMutableDictionary alloc]initWithCapacity:10];

    }
    for (int i=0; i<self.circleArray.count; i++){
        [self.selectDict setObject:@"1" forKey:[NSString stringWithFormat:@"%d",i]];
    }
    
    
    
    
    [self makeNavBarView];
    self.circleView.delegate = self;
    self.circleView.dataSource = self;
    [self.circleView setSeparatorStyle:UITableViewCellSeparatorStyleNone];//去掉系统的分割线
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - 设置导航条
-(void)makeNavBarView
{
    
    
  self.navigationItem.title = @"我的圈子管理";
    

    //贴图
    UIBarButtonItem *backBtnImg = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"公用_返回.png"] style:UIBarButtonItemStylePlain target:self action:@selector(leftButtonClicked:)];
    backBtnImg.tintColor = [UIColor whiteColor];
    self.navigationItem.leftBarButtonItem = backBtnImg;
    
    UIButton *rightButton = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 23, 23)];
    [rightButton setImage:[UIImage imageNamed:@"公用_完成"] forState:UIControlStateNormal];
    [rightButton addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    UIBarButtonItem* rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:rightButton];
    self.navigationItem.rightBarButtonItem = rightBarButtonItem;

   
    
    
}
#pragma mark -
#pragma mark -- click
-(void)leftButtonClicked:(UIButton *)sender
{

    [self.navigationController popViewControllerAnimated:YES];
    
    
}
-(void)rightButtonClick:(UIButton *)sender
{
    
    if (self.selectDict.count==0) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    

    
    for (int i=0; i<self.selectDict.count; i++) {
        if ([[self.selectDict objectForKey:[NSString stringWithFormat:@"%d",i]] isEqualToString:@"0"]) {
            [self.attentionDict setObject:[[self.circleArray objectAtIndex:i] gid] forKey:[[self.circleArray objectAtIndex:i] title]];
            
        }
    }
    
    if (0 == self.attentionDict.count) {
        [self.navigationController popViewControllerAnimated:YES];
        return;
    }
    
    NSMutableString *gids = [[NSMutableString alloc]initWithCapacity:10];
    
    for (int i=0; i<[self.attentionDict allValues].count; i++) {
        if (i==0) {
            [gids appendString:[[self.attentionDict allValues] objectAtIndex:i]];
            [gids appendString:@","];
            
            
        }else if(i==[self.attentionDict allKeys].count-1){
            [gids appendString:[[self.attentionDict allValues] objectAtIndex:i]];
        }else{
            [gids appendString:[[self.attentionDict allValues] objectAtIndex:i]];
            [gids appendString:@","];
        }
    }
    [self showActionView];
    NSMutableDictionary *dict = [[NSMutableDictionary alloc]initWithCapacity:10];
    
    [dict setObject:kPlatform forKey:@"platform"];
    [dict setObject:kVersion forKey:@"version"];
    [dict setObject:gids forKey:@"gids"];
    
    
    
    
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager POST:[NSString stringWithFormat:@"%@%@",kBaseURL,GROUP_BATCHDEL] parameters:dict success:^(AFHTTPRequestOperation *operation, id responseObject) {
        MyNSLog("%@",responseObject);
        NSNumber * statusNum = [responseObject objectForKey:@"status"];
        NSNumberFormatter* numberFormatter = [[NSNumberFormatter alloc] init];
        NSString * statusString = [numberFormatter stringFromNumber:statusNum];
        if ([statusString isEqualToString:@"0"]) {
            alertContent([responseObject objectForKey:@"msg"]);
        }else{
            
            [self.navigationController popViewControllerAnimated:YES];
            
        }
        
        [self hiddenActionView];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        [self hiddenActionView];
    }];
    


    
    
}
-(IBAction)selectButtonClick:(id)sender{
    UIButton *button =(UIButton*)sender;
    NSInteger tags = button.tag;
    if ([[self.selectDict objectForKey:[NSString stringWithFormat:@"%ld",(long)tags]] isEqualToString:@"1"]) {
        [button setImage:[UIImage imageNamed:@"添加_未添加"] forState:UIControlStateNormal];
        [self.selectDict setObject:[NSString stringWithFormat:@"%d",0] forKey:[NSString stringWithFormat:@"%ld",(long)tags]];
    }else if ([[self.selectDict objectForKey:[NSString stringWithFormat:@"%ld",(long)tags]] isEqualToString:@"0"]){
        [button setImage:[UIImage imageNamed:@"添加_添加"] forState:UIControlStateNormal];
        [self.selectDict setObject:[NSString stringWithFormat:@"%d",1] forKey:[NSString stringWithFormat:@"%ld",(long)tags]];
        
    }
    
}
#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    NSInteger number=0;
    
    number = 1;
    return number;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    // Return the number of rows in the section.
    NSInteger number=0;
    
    number = self.circleArray.count;
    return number;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    NSInteger hight=ADD_CIRCLE_SECOND_LEVEL_TABLEVIEWCELL_HIGHT;
    return hight;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    
    static NSString *ID=nil;
    AddCircleSecondLevelTableViewCell *cell = nil;
    if (kScreenWidth == 414) {
        ID=@"tableViewCell414";
    }else{
        ID=@"tableViewCell";
    }
    cell = [tableView dequeueReusableCellWithIdentifier:ID];
    
    if (cell == nil) {
        if (kScreenWidth == 414) {
            cell = [[[NSBundle mainBundle] loadNibNamed:@"AddCircleSecondLevelTableViewCell" owner:self options:nil] lastObject];

        }else{
            cell = [[[NSBundle mainBundle] loadNibNamed:@"AddCircleSecondLevelTableViewCell" owner:self options:nil] firstObject];

        }
        /**
         *  线条的处理
         */
        
        UIView *bottomLineView = [[UIView alloc]init];
        
//        if (indexPath.row != self.circleArray.count - 1)
//        {
//            bottomLineView.frame = CGRectMake(70, cell.contentView.height-0.5,kScreenWidth-70 - 5, 0.5);
//        }
//        else
//        {
            bottomLineView.frame = CGRectMake(0, cell.contentView.height-0.5, kScreenWidth, 0.5);
//        }
        
        bottomLineView.backgroundColor = [ClassMethodsViewController colorWithHexString:LINE_BACKGROUNDCOLOR_16];
        [cell.contentView addSubview:bottomLineView];

    }
    
    
    
    [[cell icon] setImageURLStr:[NSString stringWithFormat:@"%@",[[self.circleArray objectAtIndex:indexPath.row] icon]] placeholder:nil];
    
    [cell title].text = [[self.circleArray objectAtIndex:indexPath.row] title];
    [cell subtitle].text =[[self.circleArray objectAtIndex:indexPath.row] lastposttitle];
    
    [[cell thread] setTitle:[NSString stringWithFormat:@" %@",[[self.circleArray objectAtIndex:indexPath.row] threads]] forState:UIControlStateNormal];
    [[cell post] setTitle:[NSString stringWithFormat:@" %@",[[self.circleArray objectAtIndex:indexPath.row] posts]] forState:UIControlStateNormal];

    
    
    
    
    
    
    // Configure the cell...
    
    
    if ([[self.selectDict objectForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]] isEqualToString:@"0"]) {
        [[cell selectButton] setImage:[UIImage imageNamed:@"添加_未添加"] forState:UIControlStateNormal];
    }else if ([[self.selectDict objectForKey:[NSString stringWithFormat:@"%ld",(long)indexPath.row]] isEqualToString:@"1"]){
        [[cell selectButton] setImage:[UIImage imageNamed:@"添加_添加"] forState:UIControlStateNormal];
        
    }
    
    [[(AddCircleSecondLevelTableViewCell *)cell selectButton] addTarget:self action:@selector(selectButtonClick:) forControlEvents:UIControlEventTouchUpInside];
    [(AddCircleSecondLevelTableViewCell *)cell selectButton].tag = indexPath.row;
    
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    
    
    
}


- (IBAction)selectedAll:(id)sender {
    for (int i=0; i<self.circleArray.count; i++) {
        [self.selectDict setObject:@"1" forKey:[NSString stringWithFormat:@"%d",i]];
    }
    [self.circleView reloadData];
}

- (IBAction)cancelAll:(id)sender {
    for (int i=0; i<self.circleArray.count; i++) {
        [self.selectDict setObject:@"0" forKey:[NSString stringWithFormat:@"%d",i]];
    }
    [self.circleView reloadData];
}
@end
